package day20210129;

import org.junit.Test;

public class AddOne {
    public static void main(String[] args) {

        int[] d = {1,2,3,4};
        int[] x = new int[d.length+1];
        x[0] = 1;
        System.arraycopy(d,0,x,1,d.length);
        for (int i : x) {
            System.out.println(i);
        }

    }

/*    @Test
    public void test02() {
        int merchant,remainder = test01(11,10);

    }*/

/*    public int test01(int divisor , int dividend ) {
        int[] result = {divisor/dividend,divisor%dividend};
        return divisor/dividend,divisor%dividend;
    }*/


    @Test
    public void test03() {
        int[] n = {8, 9, 9, 9};
        int[] ints = plusOne(n);
        for (int s : ints) {
            System.out.println(s);
        }
    }

    public int[] plusOne(int[] digits) {
        int temp = getMerOrRem(digits[digits.length-1] + 1 , true );
        digits[digits.length-1] = getMerOrRem(digits[digits.length-1] + 1 , false) ;
        int across = temp ;
        for (int i = digits.length-2 ; i >=0 ; i-- ) {
            temp = getMerOrRem(digits[i] + across ,true) ;
            digits[i] = getMerOrRem(digits[i] + across ,false) ;
            System.out.println(i+" "+temp+" "+digits[i]);
            if (temp == 0) {
                break;
            }
            across = temp ;
        }
        if (temp == 1) {
            int[] tempArray = new int[digits.length+1] ;
            tempArray[0] = temp ;
            System.arraycopy(digits,0,tempArray,1,digits.length);
            return tempArray ;
        }
        return digits;
    }

    public int getMerOrRem(int divisor , boolean MOR) {
        if (MOR) {
            return divisor / 10 ;
        }else {
            return divisor % 10 ;
        }
    }
}
